<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Receives a message</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="zmqsocket.ispersistent.html">« ZMQSocket::isPersistent</a></li>
      <li style="float: right;"><a href="zmqsocket.recvmulti.html">ZMQSocket::recvMulti »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.zmqsocket.html">ZMQSocket</a></li>
    <li>Receives a message</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="zmqsocket.recv" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">ZMQSocket::recv</h1>
  <p class="verinfo">(PECL zmq &gt;= 0.5.0)</p><p class="refpurpose"><span class="refname">ZMQSocket::recv</span> &mdash; <span class="dc-title">Receives a message</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-zmqsocket.recv-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><strong>ZMQSocket::recv</strong></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$mode</code><span class="initializer"> = 0</span></span>): <span class="type">string</span></div>

  <p class="para rdfs-comment">
   Receive a message from a socket. By default receiving will block until a message is available unless
   <strong><code>ZMQ::MODE_DONTWAIT</code></strong> flag is used. <strong><code>ZMQ::SOCKOPT_RCVMORE</code></strong>
   socket option can be used for receiving multi-part messages. See <span class="function"><a href="zmqsocket.setsockopt.html" class="function">ZMQSocket::setSockOpt()</a></span> 
   for more information.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-zmqsocket.recv-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">mode</code></dt>

     <dd>

      <p class="para">
       Pass mode flags to receive multipart messages or non-blocking operation. 
       See <strong><code>ZMQ::MODE_*</code></strong> constants.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-zmqsocket.recv-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the message. If <strong><code>ZMQ::MODE_DONTWAIT</code></strong> 
   is used and the operation would block <strong><code>false</code></strong> shall be returned.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-zmqsocket.recv-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   Throws <span class="classname"><strong class="classname">ZMQSocketException</strong></span> on error. 
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-zmqsocket.recv-examples">
 <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4596">
    <p><strong>示例 #1 A send/recv example</strong></p>
    <div class="example-contents"><p>
     Non-blocking send / receive
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">/*&nbsp;Create&nbsp;new&nbsp;queue&nbsp;object,&nbsp;there&nbsp;needs&nbsp;to&nbsp;be&nbsp;a&nbsp;server&nbsp;at&nbsp;the&nbsp;other&nbsp;end&nbsp;*/<br /></span><span style="color: #0000BB">$queue&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ZMQSocket</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">ZMQContext</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">ZMQ</span><span style="color: #007700">::</span><span style="color: #0000BB">SOCKET_REQ</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$queue</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"tcp://127.0.0.1:5555"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Assign&nbsp;socket&nbsp;1&nbsp;to&nbsp;the&nbsp;queue,&nbsp;send&nbsp;and&nbsp;receive&nbsp;*/<br /></span><span style="color: #0000BB">$retries&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$sending&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;Start&nbsp;a&nbsp;loop&nbsp;*/<br /></span><span style="color: #007700">do&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Try&nbsp;to&nbsp;send&nbsp;/&nbsp;receive&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$sending</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Sending&nbsp;message\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$queue</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">send</span><span style="color: #007700">(</span><span style="color: #DD0000">"This&nbsp;is&nbsp;a&nbsp;message"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">ZMQ</span><span style="color: #007700">::</span><span style="color: #0000BB">MODE_DONTWAIT</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$sending&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Got&nbsp;response:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$queue</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">recv</span><span style="color: #007700">(</span><span style="color: #0000BB">ZMQ</span><span style="color: #007700">::</span><span style="color: #0000BB">MODE_DONTWAIT</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">ZMQSocketException&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;EAGAIN&nbsp;means&nbsp;that&nbsp;the&nbsp;operation&nbsp;would&nbsp;have&nbsp;blocked,&nbsp;retry&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getCode</span><span style="color: #007700">()&nbsp;===&nbsp;</span><span style="color: #0000BB">ZMQ</span><span style="color: #007700">::</span><span style="color: #0000BB">ERR_EAGAIN</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;-&nbsp;Got&nbsp;EAGAIN,&nbsp;retrying&nbsp;(</span><span style="color: #0000BB">$retries</span><span style="color: #DD0000">)\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"&nbsp;-&nbsp;Error:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">());<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Sleep&nbsp;a&nbsp;bit&nbsp;between&nbsp;operations&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">usleep</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br />}&nbsp;while&nbsp;(--</span><span style="color: #0000BB">$retries</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Sending message
 - Unable to execute operation, retrying (4)
Got response: This is a message
</pre></div>
    </div>    
   </div>
  </p>
 </div>


</div></div></div></body></html>